/*
package com.yunc.base.security.filter;

import cn.hutool.json.JSONUtil;
import com.yunc.base.security.UserEntity;
import com.yunc.base.utils.*;
import com.yunc.modules.sys.entity.SysLoginLog;
import com.yunc.modules.sys.service.ISysLoginLogService;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

*/
/**
 * 登录
 *
 * @author taocongcong
 * @create 2019-10-12 9:29
 *//*

@Slf4j
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    private ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor)SpringContextUtils.getBean("taskExecutor");
    private ISysLoginLogService sysLoginLogService = (ISysLoginLogService)SpringContextUtils.getBean("sysLoginLogService");

    private AuthenticationManager authenticationManager;

    public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
        // 配置登录路径，默认为/login
        super.setFilterProcessesUrl("/auth/login");
    }

    */
/**
     * 测试限流注解，下面配置说明该接口 60秒内最多只能访问 10次，保存到redis的键名为 limit_test，
     *//*

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        String loginName = getRequestParameter(request, "loginName");
        String userPassword = getRequestParameter(request, "userPassword");
        return authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(loginName, userPassword, new ArrayList<>()));
    }

    */
/**
     * 成功验证后调用的方法
     * 如果验证成功，就生成token并返回
     *//*

    @Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
                                            FilterChain chain, Authentication authResult) throws IOException {
        loginLog(request, BaseConstant.LOGIN_SUCCESS, BaseConstant.SuccessErrorStatus.SUCCESS.getValue());
        // 查看源代码会发现调用getPrincipal()方法会返回一个实现了`UserDetails`接口的对象
        // 所以就是JwtUser啦
        UserEntity jwtUser = (UserEntity) authResult.getPrincipal();
        // System.out.println("jwtUser:" + jwtUser.toString());
        String token = JwtTokenUtils.createToken(String.valueOf(jwtUser.getUserId()), jwtUser.getUsername(), jwtUser.getPermissions(),
                jwtUser.getRoles(), jwtUser.getDeptIds(), jwtUser.getDataScope(),true);
        // 返回创建成功的token
        // 但是这里创建的token只是单纯的token
        // 按照jwt的规定，最后请求的格式应该是 `Bearer token`
        response.setHeader("token", JwtTokenUtils.TOKEN_PREFIX + token);
        response.setContentType("application/json; charset=utf-8");
        // 需要传递user数据
        R r = R.ok().put("token", token)
                .put("userId", jwtUser.getUserId())
                .put("headLogo", jwtUser.getHeadLogo());
        log.info("用户" + jwtUser.getUsername() + "登录成功！");
        response.getWriter().write(JSONUtil.toJsonStr(r));
    }

    */
/**
     * 这是验证失败时候调用的方法
     *//*

    @Override
    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException {
        String message = failed.getMessage();
        if (failed instanceof UsernameNotFoundException) {
            message = BaseConstant.LOGIN_ERROR_NOID;
        } else if (failed instanceof BadCredentialsException) {
            message = BaseConstant.LOGIN_ERROR_PSW;
        }
        loginLog(request, message, BaseConstant.SuccessErrorStatus.ERROR.getValue());
        R r = R.error(message);
        response.setContentType("application/json; charset=utf-8");
        log.info("用户登录失败！");
        response.getWriter().write(JSONUtil.toJsonStr(r));
    }
}
*/
